package screen.item;

import globals.Global;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;

public abstract class EditField extends InteractableItem {

	protected boolean selected;
	protected String text;
	protected Font font;
	
	public EditField(String text, boolean selected, int x, int y, int width, int height, Font font) {
		this.selected = selected;
		this.x = x;
		this.y = y;
		this.width = width;
		this.height = height;
		this.font = font;
		this.text = text;
		text = "";
	}
	
	public String getText() {
		return text;
	}
	
	@Override
	public void Draw(Graphics graphics) {
		int borderSize = 1;
		
		// Draw edit box
		graphics.setColor(new Color(0f,0f,0f,1f));
		graphics.fillRect(x, y, width, height);
		if (selected)
			graphics.setColor(new Color(1f,1f,1f,1f));
		else
			graphics.setColor(new Color(0.8f,0.8f,0.8f,1f));
		graphics.fillRect(x + borderSize, y+borderSize, width - 2*borderSize, height - 2*borderSize);
		
		// Draw text
		graphics.setFont(font);
		graphics.setColor(new Color(0f,0f,0f,1f));
		graphics.drawString(text, x+2, y+height-6);
	}
	
	@Override
	public void Update(float elapsed) {
		super.Update(elapsed);
		
		if (selected) {
			if (Global.Instance().input.isBackSpace()) {
				if (text.length() > 0) {
					String sr = text.substring(0, text.length() - 1);	
					text = sr;
				}
			} else {
				text = text.concat(Global.Instance().input.isKeyTyped());
			}
		}
	}
}
